<?php
/**
 * 
 * IndexAction.class.php (前台首页)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class ProductAction extends BaseAction
{
    function _initialize()
    {	
	parent::_initialize();
    }

    public function index()
    {
        $url_s='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        $this->assign('url_s',$url_s);

        $this->Urlrule =F('Urlrule');
	    if(empty($catid)) $catid =  intval($_REQUEST['id']);
	    $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
        if($catid){
            $cat = $this->categorys[$catid];
            $bcid = explode(",",$cat['arrparentid']);
            $bcid = $bcid[1];
            if($bcid == '') $bcid=intval($catid);
            $module="Product";
            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('catid',$catid);
            $this->assign('bcid',$bcid);
        }
	    $fields = F($this->mod[$module].'_Field');
        foreach($fields as $key=>$r){
            $fields[$key]['setup'] =string2array($fields[$key]['setup']);
        }
	    $this->assign ( 'fields', $fields);


        $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$cat['keywords']);
        $this->assign ('seo_description',$cat['description']);

        //if(!$catid) $catid=27;

        if(empty($cat['listtype'])){
            $where = " status=1 ";
            if($cat['child']){
                    $where .= " and catid in(".$cat['arrchildid'].")";
            }else{
                    $where .=  " and catid=".$catid;
            }

            $itemType=$_GET['itemType'];
            $device=$_GET['device'];
            $brandid=$_GET['brandid'];
            if($itemType){
                $where .= "  and space=".$itemType;
            }
            if($device){
                $where .= "  and device=".$device;
            }
            if($brandid){
                $where .= "  and brandid=".$brandid;
            }


            $this->dao= M($module);
            $count = $this->dao->where($where)->count();
            if($count){
                    import ( "@.ORG.Page" );
                    $listRows =  !empty($cat['pagesize']) ? $cat['pagesize'] : C('PAGE_LISTROWS');
                    $page = new Page ( $count, $listRows );
                    $page->urlrule = geturl($cat,'',$this->Urlrule);
                    $pages = $page->preshow();

                    $field =  $this->module[$this->mod[$module]]['listfields'];
                    $field =  "*";
                    $orderstr='listorder ,createtime desc,id desc';
                    if($_GET['order']){
                        $orderstr=$_GET['order'].' desc';
                    }

                $list = $this->dao->field($field)->where($where)->order($orderstr)->limit($page->firstRow . ',' . $page->listRows)->select();

                    $this->assign('pages',$pages);
                    $this->assign('list',$list);
		    $this->assign('jgcount',$count);
            }

            //$brandlist=M("Brand")->where($where)->order("id")->select();
            //$this->assign('brandlist',$brandlist);
//print_r($list);exit;
            $template_r = 'list';
        }
        $template = $cat['template_list'] ? $cat['template_list'] : $template_r;
        $this->display($module.':'.$template);
    }

    public function show($id='',$module='')
    {
        $this->Urlrule =F('Urlrule');
        $p= max(intval($_REQUEST[C('VAR_PAGE')]),1);
        $id = $id ? $id : intval($_REQUEST['id']);
        $module = $module ? $module : MODULE_NAME;
        $this->assign('module_name',$module);
        $this->dao= M($module);;
        $data = $this->dao->find($id);
        if(empty($data)){
            $this->error(L('不存在,或已被删除'));
        }

        $catid = $data['catid'];
        $cat = $this->categorys[$data['catid']];
        if(empty($cat['ishtml']))$this->dao->where("id=".$id)->setInc('hits'); //添加点击次数
        $bcid = explode(",",$cat['arrparentid']);
        $jgbcid = $bcid;
        $bcid = $bcid[1];
        if($bcid == '') $bcid=intval($catid);

        if($data['readgroup']){
            if($this->_groupid!=1 && !in_array($this->_groupid,explode(',',$data['readgroup'])) )$noread=1;
        }elseif($cat['readgroup']){
            if($this->_groupid!=1 && !in_array($this->_groupid,explode(',',$cat['readgroup'])) )$noread=1;
        }
        if($noread==1){$this->assign('jumpUrl',URL('User-Login/index'));$this->error (L('NO_READ'));}

        $chargepoint = $data['readpoint'] ? $data['readpoint'] : $cat['chargepoint'];
        if($chargepoint && $data['userid'] !=$this->_userid){
            $user = M('User');
            $userdata =$user->find($this->_userid);
            if($cat['paytype']==1 && $userdata['point']>=$chargepoint){
                $chargepointok = $user->where("id=".$this->_userid)->setDec('point',$chargepoint);
            }elseif($cat['paytype']==2 && $userdata['amount']>=$chargepoint){
                $chargepointok = $user->where("id=".$this->_userid)->setDec('amount',$chargepoint);
            }else{
                $this->error (L('NO_READ'));
            }
        }

        $seo_title = $data['title'].'-'.$cat['catname'];
        $this->assign ('seo_title',$seo_title);
        $this->assign ('seo_keywords',$data['keywords']);
        $this->assign ('seo_description',$data['description']);
        $this->assign ( 'fields', F($cat['moduleid'].'_Field') );


        $fields = F($this->mod[$module].'_Field');
        //print_r($data);exit;
        foreach($data as $key=>$c_d){
            $setup='';
            $fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);

            if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
                $data[$key.'_old_val'] =$data[$key];
                $data[$key]=fieldoption($fields[$key],$data[$key]);
            }elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){
                if(!empty($data[$key])){
                    $p_data=explode(':::',$data[$key]);
                    $data[$key]=array();
                    foreach($p_data as $k=>$res){
                        $p_data_arr=explode('|',$res);
                        $data[$key][$k]['filepath'] = $p_data_arr[0];
                        $data[$key][$k]['filename'] = $p_data_arr[1];
                    }
                    unset($p_data);
                    unset($p_data_arr);
                }
            }
            unset($setup);
        }
        $this->assign('fields',$fields);


        //手动分页
        $CONTENT_POS = strpos($data['content'], '[page]');
        if($CONTENT_POS !== false) {

            $urlrule = geturl($cat,$data,$this->Urlrule);
            $urlrule =  str_replace('%7B%24page%7D','{$page}',$urlrule);
            $contents = array_filter(explode('[page]',$data['content']));
            $pagenumber = count($contents);
            for($i=1; $i<=$pagenumber; $i++) {
                $pageurls[$i] = str_replace('{$page}',$i,$urlrule);
            }
            $pages = content_pages($pagenumber,$p, $pageurls);
            //判断[page]出现的位置是否在文章开始
            if($CONTENT_POS<7) {
                $data['content'] = $contents[$p];
            } else {
                $data['content'] = $contents[$p-1];
            }
            $this->assign ('pages',$pages);
        }
        if(MODULE_NAME=="Topic"){
            //评论
            $jgcommentcount=M("Comment")->where("contype='".MODULE_NAME."' and conid=".$id)->count();
            $this->assign('jgcommentcount',$jgcommentcount);
        }
        if(!empty($data['template'])){
            $template = $data['template'];
        }elseif(!empty($cat['template_show'])){
            $template = $cat['template_show'];
        }else{
            $template =  'show';
        }

        $this->assign('jgbcid',$jgbcid);
        $this->assign('catid',$catid);
        $this->assign ($cat);
        $this->assign('bcid',$bcid);

        $this->assign ($data);
       
//print_r($data);exit;
        $this->display($module.':'.$template);
    }
}
?>